package ab.mes.controller;

import ab.mes.entity.Equipment;
import ab.mes.service.EquipmentService;
import ab.mes.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;

    @PostMapping("/getEquipment")
    public Result getEquipment(){
        Result result=new Result();
        try {
            result = equipmentService.getEquipment();
        }catch (Exception e){
            result.setErrorMessage("查询设备失败！");
            e.printStackTrace();
        }
        return result;
    }

    //通过物料编号获取物料的名字、型号和描述
    @PostMapping("/getStaffByNumber")
    public Result getEquipmentByNumber(Integer id_Of_Equipment){
        Result result=new Result();
        try {
            result = equipmentService.getEquipmentByNumber(id_Of_Equipment);
        }catch (Exception e){
            result.setErrorMessage("查询物料失败！");
            e.printStackTrace();
        }
        return result;
    }
    //新增物料信息
    @PostMapping("/insertEquipment")
    public Result insertEquipment(@RequestBody Equipment equipment){
        Result result=new Result();
        try {
            result = equipmentService.insertEquipment(equipment);
        }catch (Exception e){
            result.setErrorMessage("新增物料失败！");
            e.printStackTrace();
        }
        return result;
    }
    //根据id删除物料信息
    @PostMapping("/deleteEquipment")
    public Result deleteEquipment(Integer id_Of_Equipment){
        Result result = new Result();
        try{
            result = equipmentService.deleteEquipment(id_Of_Equipment);
        }catch (Exception e){
            result.setErrorMessage("删除物料失败！");
            e.printStackTrace();
        }
        return result;
    }
    //修改物料信息
    @PostMapping("/editEquipment")
    public Result editEquipment(@RequestBody Equipment equipment){
        Result result = new Result();
        try{
            result = equipmentService.editEquipment(equipment);
        }catch (Exception e){
            result.setErrorMessage("修改物料失败！");
            e.printStackTrace();
        }
        return result;
    }

    //模糊查询功能
    @PostMapping("/searchEquipment")
    public Result searchEquipment(String keyWord){
        Result result = new Result();
        try{
            result = equipmentService.searchEquipment(keyWord);
        }catch (Exception e){
            result.setErrorMessage("模糊查询失败！");
            e.printStackTrace();
        }
        return result;
    }
}
